<template>
  <a-card :bordered="false">
    <a-spin :spinning="confirmLoading">
      <div style="font-size: 16px; font-weight: 600; padding: 10px 0">{{ $t('接入参数') }}</div>
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="10">
              <a-form-model-item
                :label="$t('接入类型')"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="meituanAppType"
              >
                <j-dict-select-tag
                  :placeholder="$t('请选择')"
                  :isdefShow="false"
                  v-model="model.meituanAppType"
                  dictCode="meituan.app_type"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row v-if="model.meituanAppType == 0">
            <a-col :span="10">
              <a-form-model-item
                :label="$t('美团AppKey')"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="meituanAppKey"
              >
                <a-input v-model="model.meituanAppKey" :placeholder="$t('请输入美团AppKey')"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row v-if="model.meituanAppType == 0">
            <a-col :span="10">
              <a-form-model-item
                :label="$t('美团AppSecret')"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="meituanAppSecret"
              >
                <a-input v-model="model.meituanAppSecret" :placeholder="$t('请输入美团AppSecret')"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="10">
              <a-form-model-item
                :label="$t('美团点评店铺id')"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="meituanOpenShopId"
              >
                <a-input v-model="model.meituanOpenShopId" :placeholder="$t('请输入美团点评店铺id')"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="10">
              <a-row>
                <a-col :span="10" :offset="6">
                  <a-button type="primary" :disabled="confirmLoading" @click="submitForm" v-has="'BisSysParameterController:edit'">{{ $t('保存') }}</a-button>
                  <a-button
                   v-if="model.meituanAppType == 0"
                   type="primary" 
                   :disabled="confirmLoading" 
                   @click="authorize" 
                   style="margin-left: 40px"
                   v-has="'BisSysParameterController:edit'">
                   {{ $t('授权') }}
                  </a-button>
                </a-col>
              </a-row>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
      <div style="font-size: 16px; font-weight: 600; padding: 10px 0">{{ $t('核销限制') }}</div>
      <a-row style="padding-left: 16px">
        <a-col :span="24" style="margin-bottom: 10px; color: rgba(0, 0, 0, 0.85)"
          >{{ $t('每个会员每种套餐每日最大核销次数') }}：<a-input-number
            v-model="meituanDayLimit.ivalue"
            style="width: 100px"
          /><span style="color: orange">{{ $t('（0表示不限制）') }}</span></a-col
        >
        <a-col :span="24" style="margin-bottom: 10px; color: rgba(0, 0, 0, 0.85)"
          >{{ $t('每个会员每种套餐总最大核销次数') }}：<a-input-number
            v-model="meituanLimit.ivalue"
            style="width: 100px"
          /><span style="color: orange">{{ $t('（0表示不限制）') }}</span></a-col
        >
        <a-col :span="24">
          <a-button type="primary" @click="submitSet" v-has="'BisSysParameterController:edit'">{{ $t('保存') }}</a-button>
        </a-col>
      </a-row>
    </a-spin>
    <authorize-modal ref="modalForm"></authorize-modal>
    <yarn-cert-modal ref="yarnModal" @ok="conSubmit" @close="getInfo"></yarn-cert-modal>
  </a-card>
</template>
<script>
import axios from 'axios'
import { getAction, httpAction } from '@/api/manage'
import AuthorizeModal from './modules/AuthorizeModal'
import YarnCertModal from '@/views/setting/modules/YarnCertModal'

export default {
  name: 'MeiTuanSetting',
  mixins: [],
  components: {
    AuthorizeModal,
    YarnCertModal,
  },
  data() {
    return {
      confirmLoading: false,
      model: {
        idKey: '',
        meituanOpenShopId: '',
        secretKey: '',
        meituanAppSecret: '',
        appKey: '',
        meituanAppKey: '',
        typeKey: '',
        meituanAppType: '',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
      validatorRules: {
        meituanAppType: [{ required: true, message: this.$t('请选择') }],
        meituanAppKey: [{ required: true, message: this.$t('请输入美团AppKey') }],
        meituanAppSecret: [{ required: true, message: this.$t('请输入美团AppSecret') }],
        meituanOpenShopId: [{ required: true, message: this.$t('请输入美团点评店铺id') }],
      },
      meituanDayLimit: {
        name: 'meituanDayLimit',
        ivalue: 0,
        comment: '日最大核销次数-美团',
      },
      meituanLimit: {
        name: 'meituanLimit',
        ivalue: 0,
        comment: '总最大核销次数-美团',
      },
    }
  },
  created() {
    this.getInfo()
    this.getInfo2()
  },
  methods: {
    authorize() {
      let that = this
      if (that.model.meituanAppType == 0) {
        if (!that.model.meituanAppKey) {
          that.$message.warning(that.$t('请输入美团AppKey'))
          //return
        }
        that.$refs.modalForm.authorize(that.model)
      }
      if (that.model.meituanAppType == 1) {
        getAction('/meituan/bisMeituanGoods/getSession',{
          authCode: that.model.meituanOpenShopId
        }).then(res=>{
          if(res.success){
            that.$message.success(that.$t('成功'))
          }else{
            that.$message.error(res.message)
          }
        })
      }
    },
    getInfo() {
      axios.all([this.fun1(), this.fun2(), this.fun3(), this.fun6()]).then(
        axios.spread((r1, r2, r3, r4) => {
          if (r1.success) {
            this.model.idKey = r1.result.id
            this.model.meituanOpenShopId = r1.result.svalue
          }
          if (r2.success) {
            this.model.secretKey = r2.result.id
            this.model.meituanAppSecret = r2.result.svalue
          }
          if (r3.success) {
            this.model.appKey = r3.result.id
            this.model.meituanAppKey = r3.result.svalue
          }
          if (r4.success) {
            this.model.typeKey = r4.result.id
            this.model.meituanAppType = r4.result.ivalue ? 1 : 0
            localStorage.setItem('meituanAppType', this.model.meituanAppType)
          }
        })
      )
    },
    fun1() {
      return getAction('/bis/bisSysParameter/queryByName', { name: 'meituanOpenShopId' })
    },
    fun2() {
      return getAction('/bis/bisSysParameter/queryByName', { name: 'meituanAppSecret' })
    },
    fun3() {
      return getAction('/bis/bisSysParameter/queryByName', { name: 'meituanAppKey' })
    },
    fun6() {
      return getAction('/bis/bisSysParameter/queryByName', { name: 'meituanAppType' })
    },
    submitForm() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          if (that.model.idKey || that.model.secretKey || that.model.appKey || that.model.appKey) {
            that.$refs.yarnModal.edit()
            return
          }
          that.conSubmit()
        }
      })
    },
    conSubmit() {
      let that = this
      that.confirmLoading = true
      let httpurl = '/bis/bisSysParameter/saveBatch'
      let method = 'post'
      let query = []
      query.push({
        id: that.model.typeKey ? that.model.typeKey : '',
        name: 'meituanAppType',
        ivalue: that.model.meituanAppType,
        comment: '美团接入类型',
      })
      query.push({
        id: that.model.idKey ? that.model.idKey : '',
        name: 'meituanOpenShopId',
        svalue: that.model.meituanOpenShopId,
        comment: '美团OpenShopId',
      })
      if(that.model.meituanAppType==0){
        query.push({
          id: that.model.secretKey ? that.model.secretKey : '',
          name: 'meituanAppSecret',
          svalue: that.model.meituanAppSecret,
          comment: '美团AppSecret',
        })
        query.push({
          id: that.model.appKey ? that.model.appKey : '',
          name: 'meituanAppKey',
          svalue: that.model.meituanAppKey,
          comment: '美团AppSecret',
        })
      }
      httpAction(httpurl, query, method)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.getInfo()
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
    fun4() {
      return getAction('/bis/bisSysParameter/queryByName', { name: 'meituanDayLimit' })
    },
    fun5() {
      return getAction('/bis/bisSysParameter/queryByName', { name: 'meituanLimit' })
    },
    getInfo2() {
      axios.all([this.fun4(), this.fun5()]).then(
        axios.spread((r1, r2) => {
          if (r1.success) {
            this.meituanDayLimit = r1.result
          }
          if (r2.success) {
            this.meituanLimit = r2.result
          }
        })
      )
    },
    submitSet() {
      let httpurl = '/bis/bisSysParameter/saveBatch'
      let method = 'post'
      let query = []
      if (!this.meituanDayLimit.ivalue) {
        this.meituanDayLimit.ivalue = 0
      }
      if (!this.meituanLimit.ivalue) {
        this.meituanLimit.ivalue = 0
      }
      query.push(this.meituanDayLimit)
      query.push(this.meituanLimit)
      httpAction(httpurl, query, method)
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.getInfo2()
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
  },
}
</script>